import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/home.vue'
import Login from '../components/Login.vue'
import Welcome from '../components/welcome.vue'
import Users from '../views/user/user.vue';
import Rights from '../views/power/right.vue'
import Roles from '../views/power/role.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: Login
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/welcome',
    component: Home,
    children:[{
      path:'/welcome',
      component:Welcome
    },{
      path:'/users',
      component:Users
    },
    {
      path:'/rights',
      component:Rights
    },
    {
      path:'/roles',
      component:Roles
    }
  ]
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})
//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to访问的路径
  //from代表重那个路径来
  //next放行
  if (to.path == '/login') return next();
  const tokenstr = window.sessionStorage.getItem('token');
  if (!tokenstr) return next('/login');
  next()
})

export default router